﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public class OperationLocation
	{
		public OperationLocation()
		{
			this.filePath = Path.Combine(Product.UserDataLocation, "InsulationLocation.xml");
			if (!File.Exists(this.filePath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "InsulationLocation.xml"), this.filePath);
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.filePath);
			this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
		}

		public OperationLocation(string locationData)
		{
			this.filePath = Path.Combine(Product.UserDataLocation, "LocationData.xml");
			if (!File.Exists(this.filePath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "LocationData.xml"), this.filePath);
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.filePath);
			this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
		}

		public List<InsulationLocation> GetAllLocations()
		{
			List<InsulationLocation> list = new List<InsulationLocation>();
			foreach (object obj in this.xmlNodeRoot.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new InsulationLocation
				{
					LocationName = xmlNode.Attributes["Name"].Value,
					LocationMark = "省"
				});
				if (xmlNode.ChildNodes != null && xmlNode.ChildNodes.Count >= 1)
				{
					foreach (object obj2 in xmlNode.ChildNodes)
					{
						XmlNode xmlNode2 = (XmlNode)obj2;
						list.Add(new InsulationLocation
						{
							LocationName = xmlNode2.InnerText,
							LocationMark = "市"
						});
					}
				}
			}
			return list;
		}

		public LocationWeather GetWeatherByLocationName(string cityName)
		{
			LocationWeather locationWeather = new LocationWeather();
			try
			{
				foreach (object obj in this.xmlNodeRoot.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (!(xmlNode.Name != "Data") && xmlNode.Attributes[LocWeather.CityName.ToString()].Value.Equals(cityName))
					{
						locationWeather.ProvinceName = xmlNode.Attributes[LocWeather.ProvinceName.ToString()].Value;
						locationWeather.CityName = xmlNode.Attributes[LocWeather.CityName.ToString()].Value;
						locationWeather.AverageTemp = Convert.ToDouble(xmlNode.Attributes[LocWeather.AverageTemp.ToString()].Value);
						locationWeather.ConditionTemp = Convert.ToDouble(xmlNode.Attributes[LocWeather.ConditionTemp.ToString()].Value);
						locationWeather.DewPointTemp = Convert.ToDouble(xmlNode.Attributes[LocWeather.DewPointTemp.ToString()].Value);
						locationWeather.AverageMiniTemp = Convert.ToDouble(xmlNode.Attributes[LocWeather.AverageMiniTemp.ToString()].Value);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				locationWeather = null;
			}
			return locationWeather;
		}

		public InsulationLayerParam GetInsulationParam(string insulationName)
		{
			InsulationLayerParam insulationLayerParam = new InsulationLayerParam();
			try
			{
				foreach (object obj in this.xmlNodeRoot.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (!(xmlNode.Name != "Insulation") && xmlNode.Attributes[InsulationMaterial.Name.ToString()].Value.Equals(insulationName))
					{
						insulationLayerParam.Name = xmlNode.Attributes[InsulationMaterial.Name.ToString()].Value;
						insulationLayerParam.Apply = xmlNode.Attributes[InsulationMaterial.Apply.ToString()].Value;
						insulationLayerParam.CoefficientNum1 = xmlNode.Attributes[InsulationMaterial.CoefficientNum1.ToString()].Value;
						insulationLayerParam.CoefficientNum2 = xmlNode.Attributes[InsulationMaterial.CoefficientNum2.ToString()].Value;
						insulationLayerParam.Performance = xmlNode.Attributes[InsulationMaterial.Performance.ToString()].Value;
						insulationLayerParam.Range = xmlNode.Attributes[InsulationMaterial.Range.ToString()].Value;
						insulationLayerParam.Density = xmlNode.Attributes[InsulationMaterial.Density.ToString()].Value;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				insulationLayerParam = null;
			}
			return insulationLayerParam;
		}

		public List<InsulationLayerParam> GetAllInsulationMaterials()
		{
			List<InsulationLayerParam> list = new List<InsulationLayerParam>();
			try
			{
				foreach (object obj in this.xmlNodeRoot.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (!(xmlNode.Name != "Insulation"))
					{
						list.Add(new InsulationLayerParam
						{
							Name = xmlNode.Attributes[InsulationMaterial.Name.ToString()].Value,
							Apply = xmlNode.Attributes[InsulationMaterial.Apply.ToString()].Value,
							CoefficientNum1 = xmlNode.Attributes[InsulationMaterial.CoefficientNum1.ToString()].Value,
							CoefficientNum2 = xmlNode.Attributes[InsulationMaterial.CoefficientNum2.ToString()].Value,
							Performance = xmlNode.Attributes[InsulationMaterial.Performance.ToString()].Value,
							Range = xmlNode.Attributes[InsulationMaterial.Range.ToString()].Value,
							Density = xmlNode.Attributes[InsulationMaterial.Density.ToString()].Value
						});
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				list = null;
			}
			return list;
		}

		private XmlDocument xmlDoc;

		private string filePath;

		private XmlNode xmlNodeRoot;
	}
}
